<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use think\Db;
use think\Config;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];



    /**
     * @ApiTitle    (首页)
     * @ApiSummary  (首页)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/index)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1635241250",
    "data": {
        "hotGame": [
        热门游戏
            {
                "id": 2,
                "game": "地下城与勇士",
                "image": "/uploads/20211014/4d2542db5824e0ae2006dbe090240be6.jpg",
                "first": "D",
                "type": "network",
                "is_hot": 1,
                "status": "normal"
            }
        ],
        "ad": [
            {
            轮播图
                "id": 1,
                "aid": 位置,
                "image": "/uploads/20211013/6aecb7e605e2d3fca33d807306643f02.jpg",
                "bg_image":
                "title": "标题"
            },

        ],
        "article": [
            {
            新闻
                "id": 2,
                "title": "如何登录7881？"
            },

        ],
    "help": [
        {
            帮助
            "id": 2,
            "title": "如何登录7881？"
            },

        ],
        "order": [
            {
                成交记录
                "account": "账号",
                "game": 游戏,
                "title": 标题
            },
        ],
        "link": [
            {
                友情链接
                "id": 1,
                "name": "7881交易平台",
                "link": "www.7881.com",
                "status": "normal",
                "weight": 0
            }
        ],
        "love": [
            {
                猜你喜欢
                "id": 1,
                "i_image": "图片",
                "title": "标题",
                "price": "价格",
                "area": 区服
            }
        ],
        "time": "2021-10-26",
        "d_rank": [日排名],
        "m_rank": [月排名],
        "consult": [{
            咨询
             "id": 1,
            "": "图片",
            "title": "上面的",
            "reply_content": "下面的",
          }],
        "proposal": [建议],
        "complaint": [投诉]
    }

     * })
     */
    public function index()
    {
        $token = $this->request->header('token');
        // 热门游戏
        $hotGame = Db::name('game')->field('id,game,type')->where('is_hot',1)->where('status','normal')->select();
        foreach ($hotGame as $k=>$v) {
            $hotGame[$k]['info'] = Db::name('game_info')->field('id,name')->where('game_id',$v['id'])->where('gt_id',5)->where('status','normal')->limit(3)->select();
        }
        // 广告位banner
        $ad = Db::name('ad')
            ->field('id,aid,image,bg_image,title,type')
            ->where('aid',1)
            ->where('status','normal')
            ->select();
        foreach ($ad as $k=>$v) {
            $ad[$k]['image'] = config('api_image_url') . $v['image'];
            $ad[$k]['bg_image'] = config('api_image_url') . $v['bg_image'];
        }
        // 公告
        $article = Db::name('article')
            ->field('id,title')
            ->where('status','normal')
            ->order('createtime desc')
            ->limit(3)
            ->select();
        $help = Db::name('help')
            ->field('id,title')
            ->where('status','normal')
            ->order('createtime desc')
            ->limit(3)
            ->select();
        // 交易记录
        $order = Db::name('order')
            ->field('account,game,title')
            ->where('status',5)
            ->order('ontime desc')
            ->limit(10)
            ->select();
        foreach ($order as $k=>$v) {
            $order[$k]['account'] = substr($v['account'],0,3). '****' . substr($v['account'],-4);
        }
        // 用户登录
        if (!empty($token)) {
            $data['user'] = Db::name('personal')->field('id,nickname,avatar')->where('token',$token)->find();
        }
        // 日排名
        $time = date('Y-m-d',time());
        $d_rank = Db::name('order')
            ->field('id,sum(amount) as amounts,account')
            ->where('status',5)
            ->where('FROM_UNIXTIME(overtime,"%Y-%m-%d")="'.$time.'"')
            ->group('account')
            ->order('amounts desc')
            ->limit(6)
            ->select();
        foreach ($d_rank as $k=>$v) {
            $d_rank[$k]['account'] = substr($v['account'],0,3). '****' . substr($v['account'],-4);
        }
        // 月排名
        $times = date('Y-m',time());
        $m_rank = Db::name('order')
            ->field('id,sum(amount) as amounts,account,game,type,overtime')
            ->where('status',5)
            ->where('FROM_UNIXTIME(overtime,"%Y-%m")="'.$times.'"')
            ->group('account')
            ->order('amounts desc')
            ->limit(6)
            ->select();
        foreach ($m_rank as $k=>$v) {
//            $m_rank[$k]['account'] = intval((time()-$v['overtime'])/84600);
            $m_rank[$k]['account'] = substr($v['account'],0,3). '****' . substr($v['account'],-4);
        }
        // 友情链接
        $link = Db::name('link')->where('status','normal')->select();
        // 猜你喜欢
        $love = Db::name('goods')->field('id,images,price,server,title')
            ->where('goods_class_type',2)
            ->where('status',1)
            ->orderRaw('Rand()')
            ->limit(5)
            ->select();
        foreach ($love as $k=>$v) {
            $love[$k]['images'] = config('api_image_url') . $v['images'];
        }
        // 咨询
        $consult = Db::name('message')
            ->field('id,title,reply_content')
            ->where('type',1)
            ->where('status',1)
            ->order('createtime desc')
            ->limit(5)
            ->select();
        // 建议
        $proposal = Db::name('message')
            ->field('id,title,reply_content')
            ->where('type',2)
            ->where('status',1)
            ->order('createtime desc')
            ->limit(5)
            ->select();
        // 投诉
        $complaint = Db::name('message')
            ->field('id,title,reply_content')
            ->where('type',3)
            ->where('status',1)
            ->order('createtime desc')
            ->limit(5)
            ->select();
        $data['hotGame'] = $hotGame;
        $data['ad'] = $ad;
        $data['article'] = $article;
        $data['help'] = $help;
        $data['order'] = $order;
        $data['link'] = $link;
        $data['love'] = $love;
        $data['time'] = $time;
        $data['d_rank'] = $d_rank;
        $data['m_rank'] = $m_rank;
        $data['consult'] = $consult;
        $data['proposal'] = $proposal;
        $data['complaint'] = $complaint;
        $this->success('ok',$data);
    }


    /**
     * @ApiTitle    (首页搜索商品)
     * @ApiSummary  (首页搜索商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/searchGoods)
     * @ApiParams   (name="id", type="varchar", required=false, description="游戏id")
     * @ApiParams   (name="keyword", type="varchar", required=false, description="关键字")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "id": 1,
    "game": "游戏名",

     * })
     */
    public function searchGoods()
    {
        $id = $this->request->post('id');
        $keyword = $this->request->post('keyword');
        if (empty($id)) {
            $this->error('请选择游戏或者使用商品编码');
        }
        $map['gameid'] = $id;
        $map['title'] = array('like','%'.$keyword.'%');
        $map['status'] = 1;
        $res = Db::name('goods')
            ->field('id,goods_sn,uid,gamename,desc,image,images,kf_images,type_name,goods_type,title,price,plat,plat_id,operator,operator_id,area,area_id,server,server_id,cross,sku,info,stock,createtime,qq,mobile,is_warrant,is_appoint,jy_time,is_safe,is_pei,havetime,coin_ratio,coin,method')
            ->where($map)->select();
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (首页游戏搜索)
     * @ApiSummary  (首页游戏搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/all)
     * @ApiParams   (name="type", type="varchar", required=false, description="mobile:手游  network:端游")
     * @ApiParams   (name="first", type="varchar", required=false, description="大写首字母")
     * @ApiParams   (name="keyword", type="varchar", required=false, description="关键字")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "id": 1,
    "game": "游戏名",

     * })
     */
    public function all()
    {
        $type = $this->request->post('type');
        $first = $this->request->post('first');
        $keyword = $this->request->post('keyword');
        if (!empty($type)) {
            $map['type'] = $type;
        }
        if (!empty($first)) {
            $map['first'] = $first;
        }
        if (!empty($keyword)) {
            $map['game'] = array('like','%'.$keyword.'%');
        }
        $map['status'] = 'normal';
        $res = Db::name('game')->field('id,image,game,type,is_hot')->where($map)->select();
        foreach ($res as $k=>$v) {
            $res[$k]['info'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$v['id'])->where('gt_id',1)->select();
            $res[$k]['image'] = config('api_image_url') . $v['image'];
        }
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (首页条件搜索)
     * @ApiSummary  (首页条件搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/search)
     * @ApiParams   (name="id", type="varchar", required=false, description="游戏id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "id": 1,
    "pid": 父id,
    "name": 分类名

     * })
     */
    public function search()
    {
        $id = $this->request->post('id');
        $game = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id','neq',3)->select();
        $res['type'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',3)->where('status','normal')->select();
        $res['area'] = $this->getTree($game);
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (公告)
     * @ApiSummary  (公告)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/article)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "id": 1,
        "title": "标题？",
        "content": 内容
        "createtime": "2021-10-14 17:26"

     * })
     */
    public function article()
    {
        $id = $this->request->post('id');
        $res = Db::name('article')->field('id,title,content,createtime')->where('id',$id)->find();
        $res['createtime'] = date('Y-m-d H:i',$res['createtime']);
        $res['content'] = str_replace('/uploads',config('api_image_url').'/uploads',$res['content']);
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (客服)
     * @ApiSummary  (客服)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/kf)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "id": 1,
    "name": "客服名",
    "qq": 客服QQ

     * })
     */
    public function kf()
    {
        $res = Db::name('kefu')->where('status','normal')->select();
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (即时通讯获取token)
     * @ApiSummary  (即时通讯获取token)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/getToken)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="p_type", type="varchar", required=false, description="buy ,sell,ok")
     * @ApiParams   (name="o_type", type="varchar", required=false, description="1:待支付 2:待发货 3:已发货 4:交易中 5:已完成 6:已赔付 7:取消")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    token

    }
     * })
     */
    public function getToken()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('p_type');
        $types = $this->request->post('o_type');
        if ($type == 'buy') {
            if ($types == 4) {
                $map['buyid'] = $id;
                $map['status'] = array('not in',array('1','4','5','6','7','8','9','10'));
                $res = Db::name('order')
//                    ->field('id,order_sn,ontime,title,game,area,server,type,amount,admin_id,buyid,uid,status,price,type_name,buy_type')
                    ->where($map)
                    ->order('ontime desc')
                    ->paginate(5,false,['query'=>request()->param()])
                    ->each(function ($item,$key){
                        $goods = Db::name('goods')->where('id',$item['goods_id'])->find();
                        if ($goods['goods_class_type'] != 1) {
                            $item['real_coin'] = $item['num'] * $goods['coin'];
                            $specs = Db::name('attr')->where('type',$item['type_name'])->value('value');
                            $item['specs'] = $specs ? $specs : '万金';
                        }
                        //收货区服
                        $item['role_server'] = Db::name('game_info')->where('id',$item['role_server'])->value('name');
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                        $user = Db::name('personal')->where('id',$item['uid'])->find();
                        $item['userToken'] = $this->getJt($user['id'],$user['account'],$user['avatar']);
                        $item['adminToken'] = $this->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                        //卖家昵称
                        $item['othername'] = $user['nickname'];
                        return $item;
                    });
                $this->success('ok',$res);
            } elseif ($types == 5) {
                $res = Db::name('order')
//                    ->field('id,order_sn,ontime,title,game,area,server,type,amount,admin_id,buyid,uid,status,price,type_name,buy_type')
                    ->where('buyid',$id)->where('status',5)
                    ->order('overtime desc')
                    ->paginate(5,false,['query'=>request()->param()])
                    ->each(function ($item,$key){
                        $goods = Db::name('goods')->where('id',$item['goods_id'])->find();
                        if ($goods['goods_class_type'] != 1) {
                            $item['real_coin'] = $item['num'] * $goods['coin'];
                            $specs = Db::name('attr')->where('type',$item['type_name'])->value('value');
                            $item['specs'] = $specs ? $specs : '万金';
                        }
                        //收货区服
                        $item['role_server'] = Db::name('game_info')->where('id',$item['role_server'])->value('name');
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                        $user = Db::name('personal')->where('id',$item['uid'])->find();
                        $item['userToken'] = $this->getJt($user['id'],$user['account'],$user['avatar']);
                        $item['adminToken'] = $this->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                        //卖家昵称
                        $item['othername'] = $user['nickname'];
                        return $item;
                    });
                $this->success('ok',$res);
            } elseif ($types == 9) {
                $map['buyid'] = $id;
//                $map['status'] = array('not in',array('4'));
                $res = Db::name('reorder')
                    ->field('*,role_account as role')
                    ->where($map)
                    ->order('ontime desc')
                    ->paginate(5,false,['query'=>request()->param()])
                    ->each(function ($item,$key){
                        $item['real_coin'] = $item['num'];
                        //收货区服
                        $item['role_server'] = Db::name('game_info')->where('id',$item['role_server'])->value('name');
                        $user = Db::name('personal')->where('id',$item['uid'])->find();
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                        $item['adminToken'] = $this->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                        $item['userToken'] = $this->getJt($user['id'],$user['account'],$user['avatar']);
                        //卖家昵称
                        $item['othername'] = $user['nickname'];
                        return $item;
                    });
                $this->success('ok',$res);
            }
        } elseif ($type == 'sell') {
            if ($types == 5) {
                $res = Db::name('order')
//                    ->field('id,order_sn,ontime,title,game,area,server,type,amount,admin_id,buyid,uid,status,price,type_name,buy_type')
                    ->where('uid',$id)
                    ->where('status',$types)
                    ->order('overtime desc')
                    ->limit(10)
                    ->paginate(5,false,['query'=>request()->param()])
                    ->each(function ($item,$key){
                        $goods = Db::name('goods')->where('id',$item['goods_id'])->find();
                        if ($goods['goods_class_type'] != 1) {
                            $item['real_coin'] = $item['num'] * $goods['coin'];
                            $specs = Db::name('attr')->where('type',$item['type_name'])->value('value');
                            $item['specs'] = $specs ? $specs : '万金';
                        }
                        //收货区服
                        $item['role_server'] = Db::name('game_info')->where('id',$item['role_server'])->value('name');
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                        $user = Db::name('personal')->where('id',$item['buyid'])->find();
                        $item['userToken'] = $this->getJt($user['id'],$user['account'],$user['avatar']);
                        $item['adminToken'] = $this->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                        //买家昵称
                        $item['othername'] = $user['nickname'];
                        return $item;
                    });
            } elseif ($types == 3) {
                $lap['uid'] = $id;
                $lap['status'] = array('not in',array('1','2','4','5','6','7','8','9','10'));
                $res = Db::name('order')
                    // ->field('id,order_sn,ontime,title,game,area,server,type,amount,admin_id,buyid,uid,status,price,type_name,buy_type')
                    ->where($lap)
                    ->order('paytime desc')
                    ->limit(10)
                    ->paginate(5,false,['query'=>request()->param()])
                    ->each(function ($item,$key){
                        $goods = Db::name('goods')->where('id',$item['goods_id'])->find();
                        if ($goods['goods_class_type'] != 1) {
                            $item['real_coin'] = $item['num'] * $goods['coin'];
                            $specs = Db::name('attr')->where('type',$item['type_name'])->value('value');
                            $item['specs'] = $specs ? $specs : '万金';
                        }
                        //收货区服
                        $item['role_server'] = Db::name('game_info')->where('id',$item['role_server'])->value('name');
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                        $user = Db::name('personal')->where('id',$item['buyid'])->find();
                        $item['userToken'] = $this->getJt($user['id'],$user['account'],$user['avatar']);
                        $item['adminToken'] = $this->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                        //买家昵称
                        $item['othername'] = $user['nickname'];
                        return $item;
                    });
            } elseif ($types == 9) {
                $lap['uid'] = $id;
//                $map['status'] = array('not in',array('4'));
                $res = Db::name('reorder')
                    ->field('*,role_account as role')
                    ->where($lap)
                    ->order('ontime desc')
                    ->paginate(5,false,['query'=>request()->param()])
                    ->each(function ($item,$key){
                        $item['real_coin'] = $item['num'];
                        //收货区服
                        $item['role_server'] = Db::name('game_info')->where('id',$item['role_server'])->value('name');
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                        $user = Db::name('personal')->where('id',$item['buyid'])->find();
                        $item['adminToken'] = $this->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                        $item['userToken'] = $this->getJt($user['id'],$user['account'],$user['avatar']);
                        //卖家昵称
                        $item['othername'] = $user['nickname'];
                        return $item;
                    });
            } else {
                $res = Db::name('order')
//                    ->field('id,order_sn,ontime,title,game,area,server,type,amount,admin_id,buyid,uid,status,price,type_name,buy_type')
                    ->where('uid',$id)
                    ->where('status',$types)
                    ->order('paytime desc')
                    ->limit(10)
                    ->paginate(5,false,['query'=>request()->param()])
                    ->each(function ($item,$key){
                        $goods = Db::name('goods')->where('id',$item['goods_id'])->find();
                        if ($goods['goods_class_type'] != 1) {
                            $item['real_coin'] = $item['num'] * $goods['coin'];
                            $specs = Db::name('attr')->where('type',$item['type_name'])->value('value');
                            $item['specs'] = $specs ? $specs : '万金';
                        }
                        //收货区服
                        $item['role_server'] = Db::name('game_info')->where('id',$item['role_server'])->value('name');
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                        $user = Db::name('personal')->where('id',$item['buyid'])->find();
                        $item['userToken'] = $this->getJt($user['id'],$user['account'],$user['avatar']);
                        $item['adminToken'] = $this->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                        //买家昵称
                        $item['othername'] = $user['nickname'];
                        return $item;
                    });
            }
            $this->success('ok',$res);
        }
    }



    /**
     * @ApiTitle    (获取自己token)
     * @ApiSummary  (获取自己token)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/getMyToken)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    token

    }
     * })
     */
    public function getMyToken()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $user = Db::name('personal')->where('id',$id)->find();
        $res = $this->getJt($user['id'],$user['account'],$user['avatar']);
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (记录即时通讯聊天记录)
     * @ApiSummary  (记录即时通讯聊天记录)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Index/getMessage)
     * @ApiParams   (name="fromUserId", type="varchar", required=true, description="融云拿的 fromUserId")
     * @ApiParams   (name="toUserId", type="varchar", required=true, description="融云拿的 toUserId")
     * @ApiParams   (name="content", type="varchar", required=true, description="聊天内容")
     * @ApiParams   (name="order_sn", type="varchar", required=true, description="订单号")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    token

    }
     * })
     */
    public function getMessage()
    {
        $post = $this->request->post();
        $res = [
            'fromUserId'    =>  $post['fromUserId'],
            'toUserId'    =>  $post['toUserId'],
            'content'   =>  $post['content'],
            'msgTimestamp'  =>  time(),
            'order_sn'  =>  $post['order_sn']
        ];
        Db::name('timely')->insert($res);
    }



    public function getJt($id,$name,$avatar)
    {
        $url = "https://api-cn.ronghub.com/user/getToken.json";
        $postData = 'userId='.$id.'&name='.$name.'&portraitUri='.$avatar;
        $request = $this->request_post_push($url,$postData);
        $res = json_decode($request,true);
        return $res;
    }



    public function request_post_push($url = "", $postData = "") {

        // 参数为空返回状态
        if (empty($url) || empty($postData)) {
            return false;
        }

        //参数初始化
        $appKey = '8luwapkv8689l';
        $appSecret = 'UHnKDLVvlEu1H2'; // 开发者平台分配的 App Secret。

        $nonce = mt_rand(); // 获取随机数。

        $timeStamp = time();// 获取时间戳。

        $signature = sha1($appSecret.$nonce.$timeStamp);

        $httpHeader = array(

            'App-Key:'.$appKey, // 平台分配

            'Nonce:'.$nonce, // 随机数

            'Timestamp:'.$timeStamp, // 时间戳

            'Signature:'.$signature, // 签名

            'Content-Type: application/x-www-form-urlencoded',

        );

        // 初始化curl
        $ch = curl_init();
        // 设置你需要抓取的URL
        curl_setopt($ch, CURLOPT_URL, $url);
        // post提交方式
        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        // 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 设置header
        curl_setopt($ch, CURLOPT_HEADER, false);
        // 增加 HTTP Header（头）里的字段
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);

        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        // 运行curl
        $data = curl_exec($ch);
        // 关闭URL请求
        curl_close($ch);

        return $data ;
    }


    /**
     * 上传文件(不用了---走阿里云文件上传)
     * @ApiMethod (POST)
     * @param File $file 文件流
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');
        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
                //合并分片文件
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
            } elseif ($method == 'clean') {
                //删除冗余的分片文件
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
                //上传分片文件
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
        } else {
            $attachment = null;
            //默认普通上传文件
            $file = $this->request->file('file');
            try {
                $upload = new Upload($file);
                $attachment = $upload->upload();
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }

            $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
        }

    }

}
